﻿using System.Collections.Generic;
using System.Windows;
#if WPF
using System.Windows.Markup;
using System.Windows.Controls;
#else
using Windows.UI.Xaml.Markup;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.Foundation;
#endif

namespace Eternal.DataVisualization
{
    public class ChartLabel : DependencyObject
    {
        internal List<SegmentInfo> Positions = new List<SegmentInfo>();

        public DataTemplate ContentTemplate
        {
            get { return (DataTemplate)GetValue(ContentTemplateProperty); }
            set { SetValue(ContentTemplateProperty, value); }
        }

        // Using a DependencyProperty as the backing store for ContentTemplate.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ContentTemplateProperty =
            DependencyProperty.Register("ContentTemplate", typeof(DataTemplate), typeof(ChartLabel), new PropertyMetadata(null));

        internal void UpdateLabels(Canvas panel)
        {
            if (ContentTemplate != null)
            {
                foreach (var label in Positions)
                {
                    var presenter = new ContentPresenter { Content = label, ContentTemplate = ContentTemplate };
                    presenter.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
                    Canvas.SetLeft(presenter, label.Point.X - presenter.DesiredSize.Width / 2);
                    Canvas.SetTop(presenter, label.Point.Y - presenter.DesiredSize.Height / 2);
                    panel.Children.Add(presenter);
                }
            }
            else
            {
                foreach (var label in Positions)
                {
                    var text = new TextBlock { Text = label.Value.Y.ToString() };
                    text.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
                    Canvas.SetLeft(text, label.Point.X - text.DesiredSize.Width / 2);
                    Canvas.SetTop(text, label.Point.Y - text.DesiredSize.Height / 2);
                    panel.Children.Add(text);
                }
            }
        }
    }
}
